import Vue from "vue"
import Vuex from "vuex"
import router,{ routes } from "../router";


import { asyncRoutes } from "../router/asyncRoutes";
import { login ,getInfo} from "../api/user";
import {setToken,getToken} from "../ulits/token";
import getters from "./getters";
Vue.use(Vuex)

const store = new Vuex.Store({
  getters,
  state: {
    isCollapse: false,
    userInfo:null,
    routesA:routes
  },
  mutations: {
    SET_COLLAPSE(state){
      state.isCollapse = !state.isCollapse
    },
    SET_INFO(state,info){
      state.userInfo = info
    },
    REST_INFO(state){
      state.userInfo = null
    },
    SET_ROUTES(state,value){
      state.routesA = state.routesA.concat(value)
    },
    REST_ROUTES(state){
      state.routesA = routes
    }
  },
  actions:{
    //
    login({ commit },userInfo){
      return new Promise(async resolve => {
        const res = await login(userInfo)
        setToken(res.data.data.token)
        commit('SET_INFO',res.data.data)
        console.log(res)

        setTimeout(()=>{
          // asyncRoutes
          router.addRoutes(asyncRoutes)
          commit('SET_ROUTES',asyncRoutes)
          resolve()

        },2000)
      })
    },
    getInfo({ commit }){
      return new Promise(async resolve => {
        const info = await getInfo(getToken())
        commit('SET_INFO',info.data.data)
        console.log(info)
        setTimeout(()=>{
          // asyncRoutes
          router.addRoutes(asyncRoutes)
          commit('SET_ROUTES',asyncRoutes)
          resolve()

        },2000)
      })
    }
  }
})

export default store
